package com.yml.logistical.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yml.logistical.dao.LogisticalMapper;
import com.yml.logistical.dao.LogisticalResponsity;
import com.yml.logistical.dao.ShopResponsity;
import com.yml.logistical.domain.Logistical;
import com.yml.logistical.domain.LogisticalResult;
import com.yml.logistical.domain.Shop;
import com.yml.month.response.CommonCode;
import com.yml.month.response.QueryResponseResult;
import com.yml.month.response.QueryResult;
import com.yml.month.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class LogisticalService {

    @Autowired
    private LogisticalMapper logisticalMapper;

    @Autowired
    private LogisticalResponsity logisticalResponsity;


    @Autowired
    private ShopResponsity shopResponsity;

    public QueryResponseResult list(Integer page, Integer size, LogisticalResult logisticalResult) {

        PageHelper.startPage(page,size);

        Page<Logistical> list= logisticalMapper.list(logisticalResult);

        QueryResult queryResult = new QueryResult();
        queryResult.setList(list.getResult());
        queryResult.setTotal(list.getTotal());

        return  new QueryResponseResult(CommonCode.SUCCESS,queryResult);
    }

    public ResponseResult add(Logistical logistical) {

        logistical.setId(null);

        logisticalResponsity.save(logistical);

        return  new ResponseResult(CommonCode.SUCCESS);
    }

    public ResponseResult del(Integer id) {
        logisticalResponsity.deleteById(id);
        return  new ResponseResult(CommonCode.SUCCESS);
    }

    public Logistical getById(Integer id) {
        Logistical logistical =null;
        Optional<Logistical> optional = logisticalResponsity.findById(id);
        if (optional.isPresent()){
            logistical=optional.get();
        }
        return  logistical;
    }

    public ResponseResult addShop(Shop shop) {
        shop.setSid(null);
        shopResponsity.save(shop);
        return  new ResponseResult(CommonCode.SUCCESS);
    }

    public ResponseResult delShop(Integer sid) {

        shopResponsity.deleteById(sid);
        return  new ResponseResult(CommonCode.SUCCESS);
    }

    public Shop GetShopById(Integer sid) {

        Shop shop=null;
        Optional<Shop> optional = shopResponsity.findById(sid);
        if (optional.isPresent()){
            shop=optional.get();
        }
        return  shop;
    }

    public ResponseResult updateShop(Shop shop, Integer sid) {

        Optional<Shop> optional = shopResponsity.findById(sid);
        if (optional.isPresent()){
            shopResponsity.save(shop);

        }
        return  new ResponseResult(CommonCode.SUCCESS);

    }


    public QueryResponseResult listShop() {
        List<Shop> list = shopResponsity.findAll();

        QueryResult queryResult = new QueryResult();
        queryResult.setList(list);
        return  new QueryResponseResult(CommonCode.SUCCESS,queryResult);
    }
}
